﻿using per.esmool.utils.DB;
using per.esmool.utils.DB.MySQL;
using System;
using System.IO;

namespace FlowSimulate.ZentaoDB {
    public class File : IIDData<File> {

        public File() { }

        public File Copy() {
            var file = new File() {
                Id         = this.Id,
                Pathname   = this.Pathname,
                Title      = this.Title,
                Extension  = this.Extension,
                Size       = this.Size,
                ObjectType = this.ObjectType,
                ObjectID   = this.ObjectID,
                AddedBy    = this.AddedBy,
                AddedDate  = this.AddedDate,
                Downloads  = this.Downloads,
                Extra      = this.Extra,
                Deleted    = this.Deleted,
            };
            return file;
        }

        [DbField] public int      Id         { get; set; }
        [DbField] public string   Pathname   { get; set; }
        [DbField] public string   Title      { get; set; }
        [DbField] public string   Extension  { get; set; }
        [DbField] public long     Size       { get; set; }
        [DbField] public string   ObjectType { get; set; }
        [DbField] public int      ObjectID   { get; set; }
        [DbField] public string   AddedBy    { get; set; }
        [DbField] public DateTime AddedDate  { get; set; }
        [DbField] public int      Downloads  { get; set; }
        [DbField] public string   Extra      { get; set; }
        [DbField] public bool     Deleted    { get; set; }
    }

    public class FileDao() : MySQLModel<File>() {
        
        public override string TableName => "zt_file";

        public int Insert(
            string originalPath,
            string savePath,
            string uploader,
            DateTime uploadDate
        ) {
            var info = new FileInfo(originalPath);
            var fileLength = info.Length;
            var originalName = Path.GetFileName(originalPath);
            var extension = Path.GetExtension(originalName);

            var file = new File() { 
                Extension        = extension        , // <图片扩展名>
                Pathname         = savePath         , // <图片的存储路径, 真实路径需要一个 1 的前缀?>
                Title            = originalName     , // <图片原名>
                Size             = fileLength       , // <图片大小>
                AddedBy          = uploader         , // <上传账号>
                AddedDate        = uploadDate       , // <上传日期>
                ObjectType       = ""               , // <固定值>
                ObjectID         = 0                , // <固定值>
                Downloads        = 0                , // <固定值>
                Extra            = ""               , // <固定值>
                Deleted          = false            , // <固定值>
            };
            return this.Insert(file);
        }
    }
}
